/**
 *
 */
$(function () {
    $(document).keyup(function (e) {
        if (e.which == 13) {
            $('#selectBtnEnter').click();
        }
    })
});
var path = '../';
var pageListUrl = path + 'equipmentRepair/list';
var pageUrl = path + 'equipmentRepair/';

//添加弹窗
var addPanel = null;
// ---------------交互代码begin-----------------
//本页面业务逻辑
var equipmentRepair = {
    checkLogic: function (dataObj) {
        var result = "";
        var isSubmit = true;
        //清除历史错误提示
        dataObj.equipmentCodeErr = "";
        dataObj.repairmanErr = "";
        dataObj.repairmanTelErr = "";
        dataObj.repairDateErr = "";
        dataObj.causeErr = "";

        //TODO 逻辑校验，去掉不必要的:
        result = customUtils.checkLen(dataObj.equipmentCode, 1, 32, '设备编号');
        if (result.length != 0) {
            dataObj.equipmentCodeErr = result;
            isSubmit = false;
        }
        result = customUtils.checkLen(dataObj.repairman, 1, 32, '维修人');
        if (result.length != 0) {
            dataObj.repairmanErr = result;
            isSubmit = false;
        }
        if(dataObj.shopContacts != ''){
            result = customUtils.checkLen(dataObj.shopContacts, 2, 32, '门店联系人');
            if (result.length != 0) {
                dataObj.shopContactsErr = result;
                isSubmit = false;
            }
        }

        var reg = /^[0-9]{11}$/;
        if (!reg.test(dataObj.repairmanTel)) {
            dataObj.repairmanTelErr = "请填写正确的手机号";
            isSubmit = false;
        }

        if(dataObj.shopPhone !=''){
            if (!reg.test(dataObj.shopPhone)) {
                dataObj.shopPhoneErr = "请填写正确的手机号";
                isSubmit = false;
            }
        }
        console.log(reg.test(dataObj.repairmanTel))

        //TODO 日期时间的长度需要手工修改到19，仅日期的10
        result = customUtils.checkLen(dataObj.repairDate, 1, 29, '维修日期');
        if (result.length != 0) {
            dataObj.repairDateErr = result;
            isSubmit = false;
        }
        result = customUtils.checkLen(dataObj.cause, 1, 128, '原因');
        if (result.length != 0) {
            dataObj.causeErr = "故障原因不能为空并且不能超过128位";
            isSubmit = false;
        }
        //检查表单是否存在错误
        $.each(dataObj, function (key) {
            if (key.indexOf('Err') != -1 && dataObj[key].length > 0) {
                return false;
            }
        });
        return isSubmit;
    }
};

//绑定列表List---begin----所有的操作都围绕列表展开，新增、更新其他比较独立 
var dataList = new Vue({
    el: "#equipmentRepairList",
    data: {
        pageInfo: {},
        //TODO 自定义条件查询---begin--------
        condition: {
            page: 1,
            rows: customUtils.pageSize,
            id: "",
            postionCode: "",
            equipmentCode: "",
            repairman: "",
            repairmanTel: "",
            shopContacts: "",
            shopPhone: "",
            repairDate: "",

            cause: "",
            createdBy: "",
            createdDate: "",
            updatedBy: "",
            updatedDate: "",
            status: "",
            key: ""
        },
        //自定义条件查询---end--------
        selectIds: [],//多选
        showSuccess: false,//false不显示成功提示
        selectedIdx: -1,//保存操作行的索引
        isSearch: false,//init:页面初始化；search:点击查询；paging:点击翻页；changePageSize:改变页面显示条数
        selectedPageSize: customUtils.pageSize//修改页面数据显示条数
    },
    mounted: function () {
        //初始化页面数据
        this.getList('init');
    },
    // 在 `methods` 对象中定义方法,如果操作中函数的逻辑比较复杂，必须封装到dictType对象
    methods: {
        selectChkBtn: function (e) {//用于多选事件
            this.selectIds.push($(e.target).val());
        },
        //搜索、分页列表
        getList: function (eventSrc) {
            var self = this;
            //改变页面显示条数
            if (eventSrc == "changePageSize") {
                if (this.pageInfo.pageSize != this.selectedPageSize) {
                    this.pageInfo.pageSize = this.selectedPageSize;
                    $('#pagination').jqPaginator('destroy');
                    eventSrc = 'init';//重新绘制控件
                } else {
                    return;
                }
            }
            //初始化时无当前页数，后台有默认第1页，每页10条
            if (this.pageInfo != undefined && this.pageInfo.pageNum != undefined) {
                self.condition["page"] = this.pageInfo.pageNum;
                self.condition["rows"] = this.pageInfo.pageSize;
            }
            //发送请求,第一个参数http对象，第二个是url，第三个是参数，第四个是成功回调，第五个是请求失败回调
            customUtils.post(self, pageListUrl, self.condition, function (response, isSuccess) {
                if (isSuccess == false) {
                    dataList.pageInfo.pages = 0;
                    customUtils.noData('无数据', 0);
                    return;
                }
                dataList.pageInfo = response.body.pagination;
                customUtils.noData('无数据', dataList.pageInfo.pages);
                //页面初始化、搜索时，才需要执行分页控件
                if (eventSrc == 'init' || eventSrc == 'search') {
                    customUtils.paging('#pagination', dataList.pageInfo.pages, customUtils.showPage, function (num) {
                        //当前页码pageNum与将要翻页的页码num不相等才执行，否则为同一页
                        if (dataList.pageInfo.pageNum != num) {
                            dataList.pageInfo.pageNum = num;
                            dataList.getList('paging');
                        }
                    });
                }
            }, function (response) {
                console.log(response);
                dataList.pageInfo.pages = 0;
                customUtils.noData('无数据', 0);
            });
        },
        //更新状态
        deleteER: function (index) {
            var self = this;
            layer.confirm("你确定要删除这条数据么", function (idx) {
                var record = self.pageInfo.list[index];
                var param = {id: record.id};
                customUtils.post(self, pageUrl + 'deleteER', param, function (response, isSuccess) {
                    record.status = status;
                    //根据需要刷新页面数据
                    dataList.getList('no');
                }, function (response) {
                    //console.log(response.body);
                });
                layer.close(idx);
            })

        },
        //初始化更新，更新对话框
        updateInit: function (index) {
            //先显示窗口，才有加载效果
            this.addOrUpdatePanel("update");
            var record = this.pageInfo.list[index];
            this.selectedIdx = index;
            var url = pageUrl + 'id/' + record.id;
            var self = this;
            customUtils.get(self.$http, url, function (response, isSuccess) {
                if (isSuccess == false) {
                    return;
                }
                //回显表单数据
                $.each(response.body, function (key) {
                    if (addOrUpdate[key] !== undefined && response.body[key]) {
                        addOrUpdate[key] = response.body[key];
                    }
                });
                console.log(addOrUpdate.$data)
            }, function (response) {
                console.log(response);
            });
        },
        //打开添加对话框
        addOrUpdatePanel: function (flag) {
            /*if (flag == 'update') {
                $(".t1").show();
            } else {
                $(".t1").hide();
            }*/

            //清除表单数据，否则会有前一条修改数据的内容
            $.each(addOrUpdate.$data, function (key) {
                addOrUpdate[key] = "";
            });
            var tmpTitle = flag == "add" ? "添加" : "修改";
            addPanel = layer.open({
                type: 1,
                title: tmpTitle + "终端维修记录",
                area: ['680px', 'auto'],
                shadeClose: false, // 点击遮罩关闭
                content: $('#addPanel')
            });
        }, exportMaintenanceRecord: function () {
            var self = this;
            var postionCode = self.condition["postionCode"];
            var equipmentCode = self.condition["equipmentCode"];
            var repairman = self.condition["repairman"];
            var repairmanTel = self.condition["repairmanTel"];
            var repairDate = self.condition["repairDate"];
            var cause = self.condition["cause"];
            location.href = pageUrl + "exportRecord?postionCode=" + postionCode + "&equipmentCode=" + equipmentCode + "&repairman=" + repairman
                + "&repairmanTel=" + repairmanTel + "&repairDate=" + repairDate + "&cause=" + cause;
            ;
        }
    }
});
//绑定列表List---end---- 

//新增或更新---------begin-------
var addOrUpdate = new Vue({
    el: "#addPanel",
    data: {
        postionCode: "",
        equipmentCode: "",
        repairman: "",
        repairmanTel: "",
        repairDate: "",
        shopContacts: "",
        shopPhone: "",
        cause: "",
        updatedBy: "",
        param1: "",
        id: "",
        //以下是错误信息
        postionCodeErr: "",
        equipmentCodeErr: "",
        repairmanErr: "",
        repairmanTelErr: "",
        shopContactsErr: "",
        shopPhoneErr: "",
        repairDateErr: "",
        causeErr: "",
        updatedByErr: "",
        showSuccess: true,//显示成功提示
    },
    //当需要在数据变化时执行异步或开销较大的操作时，这个方式是最有用的
    watch: {},
    methods: {
        changeTime: function (elem) {
            addOrUpdate[elem.split('-')[0]] = $("#" + elem).val();
        },
        save: function () {
            //逻辑校验
            if (!equipmentRepair.checkLogic(this.$data)) {
                return;
            }
            //没有id的时候表示新增，否则为更新
            var actionUrl = this.id == "" ? 'save' : 'update';
            var self = this;
            customUtils.post(self, pageUrl + actionUrl, self.$data, function (response, isSuccess) {
                if (actionUrl == 'update') {
                    var record = dataList.pageInfo.list[dataList.selectedIdx];
                    //更新成功，刷新列表中对应的数据
                    $.each(record, function (key) {
                        if (addOrUpdate[key] != undefined) {
                            record[key] = addOrUpdate[key];
                        }
                    });
                } else if (isSuccess) {
                }
                dataList.getList('no');
                if (isSuccess && addPanel) layer.close(addPanel);
            }, function (res) {
                //console.log(response.body);
            });
        },
        //清除表单数据
        reset: function () {
            if (addPanel) layer.close(addPanel);
        }
    }
});
//新增或更新---------end-------
	
	
